package view.MainWindow.Conponents;
import Entitys.DiseaseItem;
import view.Interface.button.ClickEventHandler;
import view.Interface.button.CustomButton;

import javax.swing.*;
import java.awt.*;

public class AddingItemWindow extends JFrame {

    // 表单项对应的输入框
    private JTextField causeField;
    private JTextField symptomField;
    private JTextField medicationField;
    private JTextField departmentField;
    private JTextField dietField;
    private JTextField reliefMethodField;
    private JTextField impactField;
    private JTextField urgencyField;
    private JTextField diseaseTypeField;
    private JTextField organField;

    private void initializeFields() {
        String[] labels = {
                "原因 (cause)",
                "症状 (symptom)",
                "药物 (medication)",
                "科室 (department)",
                "饮食建议 (diet)",
                "缓解方法 (reliefmethod)",
                "影响 (impact)",
                "紧急程度 (urgency)",
                "疾病类型 (diseasetype)",
                "器官/部位 (organ)"
        };

        // 对应字段变量
        JTextField[] fields = new JTextField[labels.length];

        for (int i = 0; i < labels.length; i++) {
            JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            JLabel jLabel = new JLabel(labels[i] + ": ");
            JTextField jTextField = new JTextField(30);

            // 将输入框保存到对应的变量中
            switch (labels[i].split(" ")[0]) {
                case "原因":
                    causeField = jTextField;
                    break;
                case "症状":
                    symptomField = jTextField;
                    break;
                case "药物":
                    medicationField = jTextField;
                    break;
                case "科室":
                    departmentField = jTextField;
                    break;
                case "饮食建议":
                    dietField = jTextField;
                    break;
                case "缓解方法":
                    reliefMethodField = jTextField;
                    break;
                case "影响":
                    impactField = jTextField;
                    break;
                case "紧急程度":
                    urgencyField = jTextField;
                    break;
                case "疾病类型":
                    diseaseTypeField = jTextField;
                    break;
                case "器官/部位":
                    organField = jTextField;
                    break;
            }

            panel.add(jLabel);
            panel.add(jTextField);
            add(panel);
        }
    }
    public AddingItemWindow(DiseaseItem item) {
        // 设置窗口标题
        setTitle("添加/编辑记录");

        // 设置关闭操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 使用垂直排列的 BoxLayout
        setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

        // 初始化输入表单
        initializeFields();

        // 如果有传入 item，则填充数据
        if (item != null) {
            fillData(item);
        }

        // 添加提交按钮
        CustomButton button = new CustomButton("提交");
        ClickEventHandler ClickEventHandler = () -> {
            submitData(item);
            JOptionPane.showMessageDialog(this, "数据已提交！");
            this.dispose();
        };
        button.setOnClickListener(ClickEventHandler);
        add(button);
        add(Box.createRigidArea(new Dimension(0, 10))); // 增加一点底部间距

        // 设置窗口大小并居中显示
        pack();
        setSize(500, 600);
        setLocationRelativeTo(null); // 居中显示
        setVisible(true);
    }

    private void fillData(DiseaseItem item) {
        causeField.setText(item.Cause());
        symptomField.setText(item.Symptom());
        medicationField.setText(item.Medication());
        departmentField.setText(item.Department());
        dietField.setText(item.Diet());
        reliefMethodField.setText(item.Reliefmethod());
        impactField.setText(item.Impact());
        urgencyField.setText(item.Urgency());
        diseaseTypeField.setText(item.Diseasetype());
        organField.setText(item.Organ());
    }

    private void submitData(DiseaseItem item) {
        // 获取用户输入的数据
        item.Cause(causeField.getText());
        item.Symptom(symptomField.getText());
        item.Medication(medicationField.getText());
        item.Department(departmentField.getText());
        item.Diet(dietField.getText());
        item.Reliefmethod(reliefMethodField.getText());
        item.Impact(impactField.getText());
        item.Urgency(urgencyField.getText());
        item.Diseasetype(diseaseTypeField.getText());
        item.Organ(organField.getText());
    }

    public static void main(String[] args) {
        // 在事件调度线程中创建GUI
        SwingUtilities.invokeLater(() -> new AddingItemWindow(new DiseaseItem()));
    }
}
